<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="教研会主题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="activityTopic">
              <a-input v-model="model.activityTopic" :maxLength="100" placeholder="请输入教研会主题"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="教研会参会人员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="selectedStaffIds">
              <j-multi-select-tag v-model="model.selectedStaffIds" dictCode="sys_user,realname,id,del_flag=0 and user_identity=3" placeholder="请选择教研会参会人员"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="教研会开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="activityBeginTime">
              <j-date placeholder="请选择活动开始时间"  v-model="model.activityBeginTime" :show-time="true" date-format="YYYY-MM-DD HH:mm" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="教研会结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="activityEndTime">
              <j-date placeholder="请选择活动结束时间"  v-model="model.activityEndTime" :show-time="true" date-format="YYYY-MM-DD HH:mm" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="教研会视频" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="video">
              <j-image-upload isMultiple :number="3" accept="video/*" v-model="model.video" v-if="uploadFileShow('video')"></j-image-upload>
              <span v-if="!model.video && formDisabled">无上传</span>
              <p v-if="!formDisabled">最多上传 3 个小于<span style="margin:0 5px;color:#f00;font-size: 14px;">100M</span>的视频！</p>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="教研会图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="photo">
              <j-image-upload isMultiple   :number="3" v-model="model.photo" v-if="uploadFileShow('photo')"></j-image-upload>
              <span v-if="!model.photo&& formDisabled">无上传</span>
              <p v-if="!formDisabled">最多上传 3 张图片！</p>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="教研会音频" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <j-image-upload isMultiple  accept="audio/*" :number="3" v-model="model.audio" v-if="uploadFileShow('audio')"></j-image-upload>
              <span v-if="!model.audio&& formDisabled">无上传</span>
              <p v-if="!formDisabled">最多上传 3 个小于<span style="margin:0 5px;color:#f00;font-size: 14px;">100M</span>的音频！</p>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="教研会成果" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="activityContent">
              <j-editor v-model="model.activityContent"  v-if="uploadFileShow('activityContent')"/>
              <span v-if="!model.activityContent&& formDisabled">空</span>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="教研会成果附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="activityFile">
              <j-upload isMultiple  v-model="model.activityFile" v-if="uploadFileShow('activityFile')" :showRemoveIcon="formDisabled?false:true"></j-upload>
              <span v-if="!model.activityFile && formDisabled">无上传</span>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="活动状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="activity_status" placeholder="请选择活动状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="comments">
              <a-textarea v-model="model.comments" :maxLength="255" :autosize="{ minRows: 4 }" placeholder="请输入备注" />
              <div style="text-align: right;margin:0;line-height: 20px;" v-if="!formDisabled">{{model.comments?model.comments.length :0}}/255</div>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'SkTeachingActivityForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           activityTopic: [
              { required: true, message: '请输入教研会主题!'},
           ],
           selectedStaffIds: [
              { required: true, message: '请输入教研会参会人员!'},
           ],
           activityBeginTime: [
              { required: true, message: '请输入活动开始时间!'},
           ],
           activityEndTime: [
              { required: true, message: '请输入活动结束时间!'},
           ],
           status: [
              { required: true, message: '请输入活动状态(0-未开始,1-进行中,2-已完成)!'},
           ],
        },
        url: {
          add: "/management/skTeachingActivity/add",
          edit: "/management/skTeachingActivity/edit",
          queryById: "/management/skTeachingActivity/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
      // 是否显示 无上传 文字
      uploadFileShow(){
        return function(code){
          return (!this.model[code] && !this.formDisabled) || (this.model[code] && this.formDisabled)||(this.model[code] && !this.formDisabled)
        }
      }
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            this.$emit("modelLoading",that.confirmLoading)
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.$emit("modelLoading",that.confirmLoading)
            })
          }
         
        })
      },
    }
  }
</script>